<template>
  <el-form class="login-container"
    :model="form"
    status-icon
    :rules="rules"
    ref="form"
    label-width="100px"
  >
    <h3 class="login_title">系统登录</h3>
    <el-form-item class="username"
        label="用户名"
        label-width="80px"
        prop="username"
    >
      <el-input
          type="input"
          v-model="form.username"
          autocomplete="off"
          placeholder="请输入账号"
      ></el-input>
    </el-form-item>

    <el-form-item
        label="密码"
        label-width="80px"
        prop="password"
    >
      <el-input
          type="password"
          v-model="form.password"
          autocomplete="off"
          placeholder="请输入密码"
      ></el-input>
    </el-form-item>

    <div class="login_submit">
      <el-button @click="login" type="primary">登录</el-button>
    </div>
  </el-form>
</template>

<script>
import {getMenu}  from 'api/data'
export default {
  name: "LoginPage",
  data() {
    return{
      form: {},
      rules: {
        username: [
          { require: true, message: "请输入用户名", trigger: 'blur'},
          {
            min: 3,
            message: '用户名长度不能小于3位',
            trigger: 'blur'
          },
        ],
        password: [
          { require: true, message: "请输入密码", trigger: 'blur'},
        ]
      }
    }
  },
  methods: {
    login() {
      getMenu(this.form)
          .then(res => {
            const {code, data} = res.data;
            if (code === 200) {
              this.$store.commit('clearMenu');
              this.$store.commit('clearTag');
              this.$store.commit('setUser', this.form.username);
              this.$store.commit('setMenu', data.menu);
              this.$store.commit('setToken', data.token)
              this.$store.commit('addMenu', this.$router)
              this.$router.push({ name: 'home'})
            } else {
              this.$message.warning(data.message);
            }
          })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.login_title {
  margin: 0 auto 40px auto;
  text-align: center;
  color: #505458;
}

.login_submit {
  align-items: center;
  display: flex;
  justify-content: center;
  margin: 30px auto 10px auto;
}
</style>